define(
   [
      'dojo/_base/declare',
      'dijit/form/Form',
      'noBusinessBase/form/FormManager',
      'dijit/Tooltip'
   ],
   function(declare, Form, FormManager, Tooltip)
   {
      return declare("noBusinessBase/form/Form", [Form],
      {
         constructor:function (args)
         {
            // Call Super
            dojo.safeMixin(this, args);
            this.toolTips = [];
            var pFormManager = new FormManager({});
            pFormManager.add(this);
         },
         postscript: function(args)
         {
            // Called after constructors
            this.inherited(arguments);
         },
         destroy: function()
         {
            var pFormManager = new FormManager({});
            pFormManager.remove(this);
            this.inherited(arguments);
         },
         jsonValidate : function(data)
         {
            // Clear erros
            dojo.forEach(this.toolTips, function(pToolTip, i)
            {
               pToolTip.destroyRecursive();
            }, this);
            this.toolTips = [];
      
            // Check for bad status
            if(data.status != 'Bad')
               return true;
      
            // Display errors
            for(var i in data.errors)
            {
               if(!data.errors.hasOwnProperty(i))
                  continue;

               var pWidget = this.getChildByName(i);
               if(!pWidget)
                  continue;
      
               // Set pWidget to error state
               pWidget.state = "Error";
               pWidget._setStateClass();
               dijit.setWaiState(pWidget.focusNode, "invalid", "true");
      
               // get message
               var msg = "";
               for(var m in data.errors[i])
               {
                  if(!data.errors[i].hasOwnProperty(m))
                     continue;

                  msg = data.errors[i][m];
               }
      
               // Set tooltip if message exists
               if(msg !== "")
               {
                  var toolTip = new Tooltip({
                     connectId: pWidget.domNode,
                     label: msg,
                     showDelay: 0
                  });
                  this.toolTips.push(toolTip);
               }
            }
            return false;
         },
         getChildByName : function(name)
         {
            var childWidgets = this.getChildren();
            for(var i in childWidgets)
            {
               if(!childWidgets.hasOwnProperty(i))
                  continue;

               if(childWidgets[i].name == name)
                  return childWidgets[i];
            }
            return null;
         }
      });
   }
);
